//
// Created by jiang on 2021/12/16.
//

#include "bus.h"

bus_t bus;

void BUS_Init(bus_t *pHandle) {
    pHandle->current = 0;
    pHandle->voltage = 0;
    /* 3300000  : ADC reference voltage(uv)
     * 4096     : ADC precision
     * 11       : Visual resistance base the resistance and amplifier */
    pHandle->vGain = 3300000 / (11 * 4096);

    /* 3300000  : ADC reference voltage(uv)
     * 4096     : ADC precision
     * 0.5f     : Visual resistance base the resistance and amplifier */
    pHandle->iGain = (2 * 3300000) / 4096;

    pHandle->maxCurrent = 5500;
    pHandle->maxVoltage = 37000;
    pHandle->minVoltage = 16000;
}


void BUS_Update(bus_t *pHandle, adc_t *aHandle) {
    pHandle->voltage = (uint16_t) (pHandle->vGain * aHandle->adcValue[0]) / 1000;
    pHandle->current = (uint16_t) (pHandle->iGain * aHandle->adcValue[1]) / 1000;

    if (pHandle->current > pHandle->maxCurrent) {
        ERR_State.bits.over_current = 1;
    }
    if (pHandle->voltage > pHandle->maxVoltage)
        ERR_State.bits.voltage_high = 1;
//    else if (pHandle->voltage < pHandle->minVoltage)
//        ERR_State.bits.voltage_low = 1;
}



